#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>

using namespace std;

int main() {
	int n, ans = 0;
	int sum = 0;
	ifstream in("input.txt");
	ofstream out("output.txt");
	in >> n;
	int *tmp = new int[n];
	int *c = new int[n];
	for (int i = 0; i < n; i++) {
		in >> tmp[i];
		sum = sum + tmp[i];
	}
	int ave = sum / n;
	for (int i = 2; i <= n; i++) {
		c[i] = c[i - 1] + tmp[i - 1] - ave;
	}
	sort(c + 1, c + 1 + n);
	for (int i = 1; i <= n; i++) {
		ans = ans + abs(c[i] - c[(n + 1) / 2]);
	}
	out << ans;
	return 0;
}